package com.itbour.web.action.yjx.trade;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.yjx.trade.YjxNotifyInfoDto;
import com.itbour.service.yjx.trade.YjxNotifyInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 一键秀 消息通知管理
 * @author 李琦
 */
public class GetYjxNotifyInfoWebAction extends BaseAction {

	/**
	 */
	private static final long serialVersionUID = 1L;
	private YjxNotifyInfoService yjxNotifyInfoService;
	private String findTitleName;//	notify 标题名称
	private String findDateStart;// notify 开始时间
	private String findDateEnd;//	notify 结束时间
	private String findNotifyType;//	根据分类来查询
	private String findValid;//	消息状态 10/未开始 20/进行中 30/已结束
	
	//  消息列表管理
	private YjxNotifyInfoDto yjxNotifyInfo;// 更新用
	HashMap<String, Object> mapNotifyInfoResult = new HashMap<String, Object>();
	private List<YjxNotifyInfoDto> notifyInfoList;// notify信息
	
	
	/**	列表获取信息
	 * 	参数：消息状态/findValid
	 */
	public String getNotifyInfoPg() throws Exception{
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto)session.get("user");
		if(user!=null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String pageNumber = request.getParameter("page");
			String pageSize = request.getParameter("rows");
			Map<String, Object> param = new HashMap<String, Object>();
			if (StringUtils.isNotBlank(findTitleName)){
				param.put("title", findTitleName);
			}
			if (StringUtils.isNotBlank(findDateStart)){
				param.put("start_time", findDateStart);
			}
			if (StringUtils.isNotBlank(findDateEnd)){
				param.put("end_time", findDateEnd);
			}
			if (StringUtils.isNotBlank(findNotifyType)){
				param.put("ntfy_type", findNotifyType);
			}
			if (StringUtils.isNotBlank(findValid)){// 消息状态 
				param.put("valid",findValid);
			}
			
			// 默认只检索自己的文档([超级管理员,模版操作员]除外)
			if(!user.isSuperUser() && (user.getRole()&32)!=32) {
				param.put("user_id", 3);
			}
			
			// 翻页查询
			Page page = yjxNotifyInfoService.pageQueryStats(param, Integer.parseInt(pageNumber),
					Integer.parseInt(pageSize));
			HashMap<String, Object> resMap = new HashMap<String, Object>();
			resMap.put("total", page.getTotalCount());
			resMap.put("rows", page.getResult());
			//writeJSON(resMap);
			mapNotifyInfoResult = resMap;
		} else {
			mapNotifyInfoResult.clear();
		}
		return SUCCESS;
	}
	public String getFindValid() {
		return findValid;
	}
	public void setFindValid(String findValid) {
		this.findValid = findValid;
	}



	// 获取专题信息列表
	public String getNotifyInfoLst() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("ntfy_id", id);
		}
		notifyInfoList = yjxNotifyInfoService.selectNotifyInfo(param);
		return SUCCESS;
	}
	
	// 逻辑删除 消息内容  可多选删除 delNotify
	public void delNotify() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
		if (user != null && user.getUser_id()!=null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			String ids = request.getParameter("ids");
			try {
				yjxNotifyInfoService.delTopic(ids);
				this.writeJSON("ok");
			} catch (Exception e) {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	// 新增消息信息 1.新增APP轮播图 2.新增开机广告
	public void addNotify() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
		if (yjxNotifyInfo != null && user != null) {
			yjxNotifyInfo.setUpdate_by(user.getUser_id().toString());
	    	if(yjxNotifyInfo.getNtfy_id()!=null && yjxNotifyInfo.getNtfy_id()>0){
	    		this.updNotify(); // 修改  消息通知
	    	} else {
	    		yjxNotifyInfoService.addNotify(yjxNotifyInfo);
	    		this.writeJSON("ok");// 新建  消息通知
	    	}
		} else {
			this.writeJSON("ng");
		}
	}
	
	
	// 更新专题信息
	public void updNotify() throws Exception {
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		if(user != null && yjxNotifyInfo != null && yjxNotifyInfo.getNtfy_id()!=null) {
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	param.put("ntfy_id", yjxNotifyInfo.getNtfy_id());
	    	param.put("update_by", user.getUser_id());
	    	
	    	if(yjxNotifyInfo.getNtfy_type()>0) {
	    		param.put("ntfy_type", yjxNotifyInfo.getNtfy_type());
	    	}
	    	if(yjxNotifyInfo.getEncrypt()!=null) {
	    		param.put("encrypt", yjxNotifyInfo.getEncrypt());
	    	}
	    	if(yjxNotifyInfo.getTitle()!= null) {
	    		param.put("title", yjxNotifyInfo.getTitle());//标题
	    	}
	    	if(yjxNotifyInfo.getMessage()!= null) {
	    		param.put("message", yjxNotifyInfo.getMessage());
	    	}
	    	if(yjxNotifyInfo.getImage()!= null) {
	    		param.put("image", yjxNotifyInfo.getImage());
	    	}
	      	if(yjxNotifyInfo.getUrl()!= null) {
	    		param.put("url", yjxNotifyInfo.getUrl());
	    	}
	       	if(yjxNotifyInfo.getStart_time()!= null) {
	    		param.put("start_time", yjxNotifyInfo.getStart_time());
	    	}
	       	if(yjxNotifyInfo.getEnd_time()!= null) {
	    		param.put("end_time", yjxNotifyInfo.getEnd_time());
	    	}
	       	if(yjxNotifyInfo.getChk_freq()!= null) {
	    		param.put("chk_freq", yjxNotifyInfo.getChk_freq());
	    	}
	       	if(yjxNotifyInfo.getEmbed_url()!= null) {
	       		param.put("embed_url",  yjxNotifyInfo.getEmbed_url());
	       	}
	       	if(yjxNotifyInfo.getWidth()!= null) {
	       		param.put("width",  yjxNotifyInfo.getWidth());
	       	}
	       	if(yjxNotifyInfo.getHeight()!= null) {
	       		param.put("height",  yjxNotifyInfo.getHeight());
	       	}
	       	if(yjxNotifyInfo.getDisp_type()!= null) {
	       		param.put("disp_type",  yjxNotifyInfo.getDisp_type());
	       	}
	    	yjxNotifyInfoService.updNotify(param);
	    	this.writeJSON("ok");
	    	
		} else {
			this.writeJSON("ng");
		}
	}
	
	//  修改专题显示顺序
	public void updNotifyOrder() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		String id=request.getParameter("orderid");
		String order=request.getParameter("number"); // 轮播图  显示顺序
		if (StringUtils.isNotBlank(id)) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	// update
	    	if(user!=null && user.getUser_id()!=null){
		    	Map<String, Object> param = new HashMap<String, Object>();
				param.put("order", Integer.valueOf(order)); // 序号值
				param.put("ntfy_id", Long.valueOf(id));// 消息ID
				param.put("update_by", user.getUser_id());
				yjxNotifyInfoService.updNotify(param);
	    	}
		}
		this.writeJSON("ok");
	}
	
	
	// 以下 是生成的set 和 get方法
	public HashMap<String, Object> getMapNotifyInfoResult() {
		return mapNotifyInfoResult;
	}
	public void setMapNotifyInfoResult(HashMap<String, Object> mapNotifyInfoResult) {
		this.mapNotifyInfoResult = mapNotifyInfoResult;
	}
	public String getFindTitleName() {
		return findTitleName;
	}
	public void setFindTitleName(String findTitleName) {
		this.findTitleName = findTitleName;
	}
	public String getFindDateStart() {
		return findDateStart;
	}
	public String getFindDateEnd() {
		return findDateEnd;
	}
	public void setFindDateStart(String findDateStart) {
		this.findDateStart = findDateStart;
	}
	public void setFindDateEnd(String findDateEnd) {
		this.findDateEnd = findDateEnd;
	}
	
	public String getFindNotifyType() {
		return findNotifyType;
	}

	public void setFindNotifyType(String findNotifyType) {
		this.findNotifyType = findNotifyType;
	}
	public YjxNotifyInfoService getYjxNotifyInfoService() {
		return yjxNotifyInfoService;
	}
	public void setYjxNotifyInfoService(YjxNotifyInfoService yjxNotifyInfoService) {
		this.yjxNotifyInfoService = yjxNotifyInfoService;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public YjxNotifyInfoDto getYjxNotifyInfo() {
		return yjxNotifyInfo;
	}
	public void setYjxNotifyInfo(YjxNotifyInfoDto yjxNotifyInfo) {
		this.yjxNotifyInfo = yjxNotifyInfo;
	}
	public List<YjxNotifyInfoDto> getNotifyInfoList() {
		return notifyInfoList;
	}
	public void setNotifyInfoList(List<YjxNotifyInfoDto> notifyInfoList) {
		this.notifyInfoList = notifyInfoList;
	}
}
